<template>
  <view class="uni-forms">
    <form @submit.stop="submitForm" @reset="resetForm">
      <slot></slot>
    </form>
  </view>
</template>

<script>
/**
   * Forms 表单
   * @description 由输入框、选择器、单选框、多选框等控件组成，用以收集、校验、提交数据
   * @tutorial https://ext.dcloud.net.cn/plugin?id=2773
   * @property {Object} rules                表单校验规则
   * @property {String} validateTrigger = [bind|submit]  校验触发器方式 默认 submit 可选
   *   @value bind   发生变化时触发
   *   @value submit   提交时触发
   * @property {String} labelPosition = [top|left]        label 位置 默认 left 可选
   * @value top    顶部显示 label
   * @value left    左侧显示 label
   * @property {String} labelWidth                label 宽度，默认 65px
   * @property {String} labelAlign = [left|center|right]    label 居中方式  默认 left 可选
   *   @value left    label 左侧显示
   *   @value center  label 居中
   *   @value right    label 右侧对齐
   * @property {String} errShowType = [undertext|toast|modal]  校验错误信息提示方式
   *   @value undertext  错误信息在底部显示
   *   @value toast    错误信息toast显示
   *   @value modal    错误信息modal显示
   */
import Vue from 'vue'

import Validator from './validate.js'
Vue.prototype.binddata = function (name, value, formName) {
  if (formName) {
    this.$refs[formName].setValue(name, value)
  } else {
    let formVm
    for (const i in this.$refs) {
      const vm = this.$refs[i]
      if (vm && vm.$options && vm.$options.name === 'uniForms') {
        formVm = vm
        break
      }
    }
    if (!formVm) return console.error('当前 uni-froms 组件缺少 ref 属性')
    formVm.setValue(name, value)
  }
}

export default {
  name: 'uniForms',
  props: {
    value: {
      type: Object,
      default () {
        return {}
      }
    },
    // 表单校验规则
    rules: {
      type: Object,
      default () {
        return {}
      }
    },
    // 校验触发器方式，默认 关闭
    validateTrigger: {
      type: String,
      default: ''
    },
    // label 位置，可选值 top/left
    labelPosition: {
      type: String,
      default: 'left'
    },
    // label 宽度，单位 px
    labelWidth: {
      type: [String, Number],
      default: 65
    },
    // label 居中方式，可选值 left/center/right
    labelAlign: {
      type: String,
      default: 'left'
    },
    errShowType: {
      type: String,
      default: 'undertext'
    }
  },
  data () {
    return {
      formData: {}
    }
  },
  watch: {
    rules (newVal) {
      this.init(newVal)
    },
    trigger (trigger) {
      this.formTrigger = trigger
    },
    value: {
      handler (newVal) {
        if (this.isChildEdit) {
          this.isChildEdit = false
          return
        }
        this.childrens.forEach((item) => {
          if (item.name) {
            const formDataValue = newVal.hasOwnProperty(item.name) ? newVal[item.name] : null
            this.formData[item.name] = this._getValue(item, formDataValue)
          }
        })
      },
      deep: true
    }
  },
  created () {
    const _this = this
    this.childrens = []
    this.formRules = []
    this.init(this.rules)
  },
  methods: {
    init (formRules) {
      if (Object.keys(formRules).length > 0) {
        this.formTrigger = this.trigger
        this.formRules = formRules
        if (!this.validator) {
          this.validator = new Validator(formRules)
        }
        this.childrens.forEach((item) => {
          item.init()
        })
      }
    },
    /**
       * 设置校验规则
       * @param {Object} formRules
       */
    setRules (formRules) {
      this.init(formRules)
    },
    /**
       * 公开给用户使用
       * 设置自定义表单组件 value 值
       *  @param {String} name 字段名称
       *  @param {String} value 字段值
       */
    setValue (name, value, callback) {
      const example = this.childrens.find(child => child.name === name)
      if (!example) return null
      this.isChildEdit = true
      value = this._getValue(example, value)
      this.formData[name] = value
      example.val = value
      this.$emit('input', Object.assign({}, this.value, this.formData))
      return example.triggerCheck(value, callback)
    },

    /**
       * TODO 表单提交， 小程序暂不支持这种用法
       * @param {Object} event
       */
    submitForm (event) {
      const value = event.detail.value
      return this.validateAll(value || this.formData, 'submit')
    },
    /**
       * 表单重置
       * @param {Object} event
       */
    resetForm (event) {
      this.childrens.forEach(item => {
        item.errMsg = ''
        item.val = ''
        item.$emit('input', '')
      })

      this.isChildEdit = true
      this.childrens.forEach((item) => {
        if (item.name) {
          this.formData[item.name] = this._getValue(item, '')
        }
      })

      this.$emit('input', this.formData)
      this.$emit('reset', event)
    },

    /**
       * 触发表单校验，通过 @validate 获取
       * @param {Object} validate
       */
    validateCheck (validate) {
      if (validate === null) validate = null
      this.$emit('validate', validate)
    },
    /**
       * 校验所有或者部分表单
       */
    async validateAll (invalidFields, type, callback) {
      if (!this.validator) {
        this.$emit('submit', {
          detail: {
            value: invalidFields,
            errors: null
          }
        })
        return
      }
      this.childrens.forEach(item => {
        item.errMsg = ''
      })

      let promise
      if (callback && typeof callback !== 'function' && Promise) {
        promise = new Promise((resolve, reject) => {
          callback = function (valid, invalidFields) {
            !valid ? resolve(invalidFields) : reject(valid)
          }
        })
      }

      const fieldsValue = {}
      const tempInvalidFields = Object.assign({}, invalidFields)

      Object.keys(this.formRules).forEach(item => {
        const values = this.formRules[item]
        const rules = (values && values.rules) || []
        let isNoField = false
        for (let i = 0; i < rules.length; i++) {
          const rule = rules[i]
          if (rule.required) {
            isNoField = true
            break
          }
        }

        // 如果存在 required 才会将内容插入校验对象
        if (!isNoField && (!tempInvalidFields[item] && tempInvalidFields[item] !== false)) {
          delete tempInvalidFields[item]
        }
      })
      // 循环字段是否存在于校验规则中
      for (const i in this.formRules) {
        for (const j in tempInvalidFields) {
          if (i === j) {
            fieldsValue[i] = tempInvalidFields[i]
          }
        }
      }

      let result = await this.validator.invokeValidateUpdate(fieldsValue, true)

      if (Array.isArray(result)) {
        if (result.length === 0) result = null
      }
      let example = null

      if (result) {
        for (let i = 0; i < result.length; i++) {
          const item = result[i]
          example = this.childrens.find(child => child.name === item.key)
          if (this.errShowType === 'undertext') {
            if (example) example.errMsg = item.errorMessage
          } else {
            if (this.errShowType === 'toast') {
              uni.showToast({
                title: item.errorMessage || '校验错误',
                icon: 'none'
              })
              break
            } else if (this.errShowType === 'modal') {
              uni.showModal({
                title: '提示',
                content: item.errorMessage || '校验错误'
              })
              break
            } else {
              if (example) example.errMsg = item.errorMessage
            }
          }
        }
      }

      if (type === 'submit') {
        this.$emit('submit', {
          detail: {
            value: invalidFields,
            errors: result
          }
        })
      } else {
        this.$emit('validate', result)
      }
      callback && typeof callback === 'function' && callback(!result, result || invalidFields)
      if (promise && callback) return promise
    },

    /**
       * 外部调用方法
       * 手动提交校验表单
       * 对整个表单进行校验的方法，参数为一个回调函数。
       */
    submit () {
      return this.validateAll(this.formData, 'submit')
    },

    /**
       * 外部调用方法
       * 校验表单
       * 对整个表单进行校验的方法，参数为一个回调函数。
       */
    validate (callback) {
      return this.validateAll(this.formData, '', callback)
    },

    /**
       * 部分表单校验
       * @param {Object} props
       * @param {Object} cb
       */
    validateField (props, callback) {
      props = [].concat(props)
      let invalidFields = {}
      this.childrens.forEach(item => {
        // item.parentVal((val, name) => {
        if (props.indexOf(item.name) !== -1) {
          invalidFields = Object.assign({}, invalidFields, {
            [item.name]: this.formData[item.name]
          })
        }
        // })
      })
      return this.validateAll(invalidFields, '', callback)
    },

    /**
       * 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
       */
    resetFields () {
      this.resetForm()
    },

    /**
       * 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组，如不传则移除整个表单的校验结果
       */
    clearValidate (props) {
      props = [].concat(props)
      this.childrens.forEach(item => {
        if (props.length === 0) {
          item.errMsg = ''
        } else {
          if (props.indexOf(item.name) !== -1) {
            item.errMsg = ''
          }
        }
      })
    },
    // 把 value 转换成指定的类型
    _getValue (item, value) {
      const rules = item.formRules.rules || []
      const isRuleNum = rules.find(val => val.format && this.type_filter(val.format))
      const isRuleBool = rules.find(val => val.format && val.format === 'boolean' || val.format === 'bool')
      // 输入值为 number
      if (isRuleNum) {
        value = value === '' || value === null ? null : Number(value)
      }
      // 简单判断真假值
      if (isRuleBool) {
        value = !!value
      }
      return value
    },
    // 过滤数字类型
    type_filter (format) {
      return format === 'int' || format === 'double' || format === 'number'
    }
  }
}
</script>

<style lang="scss">
</style>
